package com.wt.admin.controller.image;


import com.aizuda.easy.security.domain.Rep;
import com.wt.admin.config.aspect.annotation.LogAno;
import com.wt.admin.domain.dto.image.ImageVideoDTO;
import com.wt.admin.domain.vo.image.ImageItemVO;
import com.wt.admin.domain.vo.image.ImageVideoVO;
import com.wt.admin.domain.vo.video.VideoTrainingVO;
import com.wt.admin.service.image.ImageProxyService;
import com.wt.admin.util.PageUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("video")
public class ImageVideoController {

    @Resource
    private ImageProxyService imageProxyService;


    @LogAno(name = "查询视频列表")
    @PostMapping("videoList")
    public Rep<PageUtil.PageVO<ImageVideoVO>> videoList(@RequestBody PageUtil.PageDTO<ImageVideoDTO> data){
        return Rep.ok(imageProxyService.getImageVideoService().videoList(data));
    }

    @LogAno(name = "删除视频")
    @PostMapping("delVideo")
    public Rep<ImageVideoVO> delVideo(@RequestBody List<String> data){
        return Rep.ok(imageProxyService.delVideo(data));
    }

    @LogAno(name = "查询对应图像")
    @PostMapping("videoByImages")
    public Rep<List<ImageItemVO>> videoByImages(@RequestBody ImageVideoDTO data){
        return Rep.ok(imageProxyService.videoByImages(data));
    }

    @PostMapping("/training")
    public Rep<VideoTrainingVO> training(@RequestParam("file") MultipartFile file, @RequestParam("model") String model) throws IOException {
        return Rep.ok(imageProxyService.videoTraining(file,model));
    }

    @Deprecated
    @GetMapping("/videoTraining")
    public String videoTraining(@RequestParam("url") String url, @RequestParam("id") Integer id) throws IOException {
        // 1. 添加分块传输支持（避免客户端等待完整数据）
//        response.setHeader("Content-Transfer-Encoding", "chunked");
//        // 2. 设置合理超时（单位毫秒）
//        response.setBufferSize(1024); // 调整缓冲区大小
//        response.setIntHeader("Keep-Alive", 999999999); // 保持连接时间
//        response.setContentType("video/flv");
//        response.setHeader(HttpHeaders.ACCEPT_RANGES, "bytes");
//        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setHeader("Access-Control-Allow-Headers", "Content-Type");
//        response.setHeader("Content-Security-Policy", "default-src'self'; media-src *;");
//        response.setHeader(HttpHeaders.CONTENT_RANGE, "bytes " + start + "-" + end + "/" + videoLength);
//        response.setContentLengthLong(contentLength);
        return imageProxyService.videoTraining(url,id);
    }

}
